package com.edisoncgh.controller;

import com.edisoncgh.pojo.CommentInfo;
import com.edisoncgh.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/commentsOption")
public class CommentsController {
    @Autowired
    private CommentsService commentsService;

    @RequestMapping("/addComment")
    public String addComment(HttpServletRequest request) {
        String nickname = request.getParameter("nickname");
        String user_comments = request.getParameter("user_comments");
//        调试
        System.out.println("nickname:" + nickname + " " + "user_comments:" + user_comments);

        CommentInfo commentInfo = new CommentInfo();
        commentInfo.setId(nickname);
        commentInfo.setAuthor(nickname);
        commentInfo.setCom_text(user_comments);
        commentInfo.setArt_id((Integer) request.getSession().getAttribute("comment_art_id"));
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        //获取当前时间String类型
        String time_string = sdf.format(date);
        Date time_date = null;
        try {
            time_date = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        commentInfo.setCom_date(time_date);

        if (commentsService.insertComment(commentInfo)){
            System.out.println("发表文章成功");
        }else {
            System.out.println("发表文章失败");
        }
        return "articledetail";
    }

//    @RequestMapping("/allComments")
//    public String findAllComment(Model model,int art_id){
//        List<CommentInfo> list = commentsService.findByArtId(art_id);
//        model.addAttribute("list",list);
//        return "articledetail";
//    }
}
